﻿using KM.Framework.Utility;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace KM.Framework.Business
{
   public class KeyboardViewModel : ViewModelBase
    {
        #region 绑定数据


     
        private string _ResultStr;
        public string ResultStr
        {
            get { return _ResultStr; }
            set
            {
                _ResultStr = value;
                this.RaisePropertyChanged("ResultStr");
            }
        }

        private bool _letterIsEnabled;
        /// <summary>
        /// 字母锁定 数字键盘
        /// </summary>
        public bool letterIsEnabled
        {
            get { return _letterIsEnabled; }
            set
            {
                _letterIsEnabled = value;
                this.RaisePropertyChanged("letterIsEnabled");
            }
        }
        private bool _IsCaps;
        /// <summary>
        /// 是否大写
        /// </summary>
        public bool IsCaps
        {
            get { return _IsCaps; }
            set
            {
                _IsCaps = value;
                this.RaisePropertyChanged("IsCaps");
            }
        }
        #endregion 

        #region 命令

        public DelegateCommand<Button> KeyClickCommand { get; set; }



        #endregion
        public KeyboardView MyView { get; set; }
        public KeyboardViewModel(KeyboardView v,string cValue,bool IsNumberKeyboard=false)
        {
            MyView = v;
            letterIsEnabled = true;
            if (IsNumberKeyboard)
            {
                letterIsEnabled = false;
            }
            IsCaps = false;
            //if (cValue.Length > 0)
            //{
            //    ResultStr = cValue;
            //}
            Init();
        }
        private void Init()
        {
            InitCommand();
        }
        private void InitCommand()
        {
            KeyClickCommand = CreateCommand<Button>((g) =>
            {
                Button btn = g as Button;
                var btnName = btn.Content.ToStringV1();
                if (btnName== "退格")
                {
                    if (ResultStr.ToStringV1().Length > 0)
                    {
                        ResultStr = ResultStr.Substring(0, ResultStr.Length - 1);
                        return;
                    }
                    else
                        return;
                }
                if (btnName == "清空")
                {
                    ResultStr = string.Empty;
                    return;
                }
                if (btnName == "确认")
                {
                    MyView.boxValue = ResultStr;
                    MyView.DialogResult = true;
                    MyView.Close();
                }
                if (btnName== "CapsLock")
                {
                    if (IsCaps)
                        IsCaps = false;
                    else
                        IsCaps = true;
                    return;
                }
                if (IsCaps)
                    btnName = btnName.ToUpper();
                else
                    btnName = btnName.ToLower();
                ResultStr += btnName;
            });

        }


     
    }
}
